<?php
declare(strict_types=1);

namespace app\admin\service\sys;

use app\admin\model\AdminRole;

/**
 * 管理员角色组 逻辑处理 服务类
 * 列表 添加 编辑 状态改变等
 **/
class RoleService
{
    /**
     * 添加 角色组
     */
    public static function addAdminRole($params)
    {
        //id 大于0 时为编辑
        if (isset($params['id']) && $params['id'] > 0) {

            //编辑
            $params['state'] = $params['state'] ? 1 : 0;
            $model = AdminRole::field('id,name,des,state')->find($params['id']);
            if ($model) {
                $model->save($params);
                return suc('编辑成功',$params);
//                return suc('编辑成功',$model);
            }
            return suc('非法操作','',1);
        }
        //添加
        unset($params['id']);
        $model = new AdminRole();
        $params['state'] = 1;
        $model->save($params);
        return suc('编辑成功',$model);
    }
    /**
     *角色权限添加/修改
     */
    public static function addAdminRoleRules($params)
    {
        $model = AdminRole::field('id,name,des,state,rules')->where('state',1)->find($params['id']);
        if($model){
            $model->rules = implode(',',$params['rules']);
//            if(!empty($params['pidRules'])){
               $pidRules =  array_merge($params['pidRules'],$params['rules']);
                $model->rulesArr = implode(',',$pidRules);;
                unset($params['pidRules']);
//            }

            $model->rules = implode(',',$params['rules']);
            $model->save();
            return true;
        }
        return false;
    }
}
